package com.caseProcess.controller;

import com.caseProcess.entity.*;
import com.caseProcess.service.ResourcesService;
import com.caseProcess.utils.FileUpload;
import com.caseProcess.utils.IdWorker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ahuo
 * @version 1.0
 * @date 2020年11月15日 15点15分
 */
@ImportResource("classpath:/springmvc-servlet.xml")
@RestController
@RequestMapping("/resources")
public class ResourcesController {

    @Value("${Upload_PoliceFile}")
    private String Upload_PoliceFile;

    IdWorker idWorker=new IdWorker(1,1,1);


    @Resource
    ResourcesService resourcesService;

    /**
     * 查询资源信息
     * @param operNoteId
     * @return
     */
    @CrossOrigin
    @RequestMapping("/select")
    public Result selectResources(String operNoteId){
        Result result = new Result();
        List<Resources> resources=new ArrayList<Resources>();
        Resources resource= new Resources();
        CaseOperNote caseOperNote=new CaseOperNote();
        caseOperNote.setOperNoteId(operNoteId);
        resource.setCaseOperNote(caseOperNote);
        resources= resourcesService.selectResourcess(resource);
        result.setData(resources);
        result.setCount(resources.size());
        return result;
    }


    /**
     * 文件上传,layui的文件上传为多文件列表上传的话,也同样使用MultipartFile file即可,它原理是多次调用同一个接口
     * @param file
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/fileUpload")
    public Result fileUpload(MultipartFile file, String processId, HttpServletRequest request){
        Result result=new Result();
        if (file.isEmpty()) {
            result.setMsg("文件上传异常");
            result.setCode(result.RESULT_ERROR);
        } else{
            Object object=SessionController.getSession(request,"police");
            if(object==null){
                result.setCode(-500);
                result.setMsg("用户信息错误,请重新登陆");
                return result;
            }
            VUserInfo vUserInfo= (VUserInfo) object;
            Map<String,String> map=new HashMap<String, String>();
            map = FileUpload.upload(file, Upload_PoliceFile+vUserInfo.getPoliceId()+"/", request);
            Resources resources = new Resources();
            resources.setResourcesId(idWorker.nextId() + "");
            CaseOperNote caseOperNote=new CaseOperNote();
            caseOperNote.setOperNoteId(processId);
            resources.setCaseOperNote(caseOperNote);
            resources.setResourcesName(file.getOriginalFilename());
            resources.setResourcesUrl(map.get("fileRelativePath"));
            int i=resourcesService.insert(resources);
            Map<String, String> remap = new HashMap<String, String>();
            remap.put("url", map.get("fileRelativePath"));
            if(i==0){
                result.setCode(result.RESULT_ERROR);
                result.setMsg("上传失败");
            }else{
                result.setMsg("上传成功");
            }
        }

        return result;
    }

}
